﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Orchard.ContentManagement;
using Orchard.ContentManagement.MetaData;
using Orchard.ContentManagement.MetaData.Builders;
using Orchard.ContentManagement.MetaData.Models;
using Orchard.ContentManagement.ViewModels;

namespace Contrib.VimeoField.Settings
{
    public class VimeoFieldEditorEvents : ContentDefinitionEditorEventsBase
    {
        public override IEnumerable<TemplateViewModel> PartFieldEditor(ContentPartFieldDefinition definition)
        {
            if (definition.FieldDefinition.Name == "VimeoField")
            {
                var model = definition.Settings.GetModel<VimeoFieldSettings>();
                yield return DefinitionTemplate(model);
            }            
        }

        public override IEnumerable<TemplateViewModel> PartFieldEditorUpdate(ContentPartFieldDefinitionBuilder builder, IUpdateModel updateModel)
        {
            var model = new VimeoFieldSettings();
            if (builder.FieldType != "VimeoField")
            {
                yield break;
            }
            if (updateModel.TryUpdateModel(
            model, "VimeoFieldSettings", null, null))
            {
                builder.WithSetting("VimeoFieldSettings.ControlsColor",model.ControlsColor);
                builder.WithSetting("VimeoFieldSettings.AutoPlay",model.Autoplay.ToString());
                builder.WithSetting("VimeoFieldSettings.Loop", model.Loop.ToString());
                builder.WithSetting("VimeoFieldSettings.ShowTitle", model.ShowTitle.ToString());
                builder.WithSetting("VimeoFieldSettings.ShowByline", model.ShowByline.ToString());
                builder.WithSetting("VimeoFieldSettings.ShowUserPortrait", model.ShowUserPortrait.ToString());
                builder.WithSetting("VimeoFieldSettings.PlayerId", model.PlayerId);
                builder.WithSetting("VimeoFieldSettings.JavascriptApiEnable", model.JavascriptApiEnable.ToString());
                builder.WithSetting("VimeoFieldSettings.DefaultWidth", model.DefaultWidth.ToString());
                builder.WithSetting("VimeoFieldSettings.DefaultHeight", model.DefaultHeight.ToString());
            }
            yield return DefinitionTemplate(model);
        }
    }
}